package com.example.one.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.one.mapper.WalletMapper;
import com.example.one.pojo.Wallet;
import com.example.one.pojo.WalletDetails;
import com.example.one.service.WalletDetailService;
import com.example.one.service.WalletService;
import com.example.one.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;

/**
 * Copyright (C), 2017-2022, MIDOU
 * ClassName: WalletServiceImpl
 * Author:   pangshu
 * Date:     2022/10/28 18:20
 * Version: 1.0
 * Description:
 */
@Service
public class WalletServiceImpl implements WalletService {

    @Autowired
    private WalletMapper walletMapper;
    @Autowired
    private WalletDetailService detailService;
    //支付:需要考虑事务，或者分布式事务
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean pay(Integer payerId,Float amount, Integer comeId) {
        Wallet wallet = getBalance(payerId);
        //1.为支付方，减去金额
        Float payBalance = wallet.getBalance();
        wallet.setBalance(payBalance - amount);
        walletMapper.updateById(wallet);
        //2.为收款方，添加金额
        Wallet balance = getBalance(comeId);
        balance.setBalance(balance.getBalance() + amount);
        int update = walletMapper.updateById(balance);
        if (update==1){

                WalletDetails walletDetails = new WalletDetails();
                walletDetails.setAmount(amount);//金额
                walletDetails.setComeId(comeId);//收款方
                walletDetails.setPayerId(payerId);//付款方
                walletDetails.setOneType(1);//操作成功
                walletDetails.setOperationTime(new Date(System.currentTimeMillis()));//时间
                walletDetails.setOperationType(1);//支付
                Integer integer = detailService.insertWalletDetail(walletDetails);
                return true;
        }else {
            WalletDetails walletDetails = new WalletDetails();
            walletDetails.setAmount(amount);//金额
            walletDetails.setComeId(comeId);//收款方
            walletDetails.setPayerId(payerId);//付款方
            walletDetails.setOneType(0);//操作失败
            walletDetails.setOperationTime(new Date(System.currentTimeMillis()));//时间
            walletDetails.setOperationType(1);//支付
            Integer integer = detailService.insertWalletDetail(walletDetails);
            return false;
        }
    }
    //退款:方法的内部调用可能出现事务失效的情况。
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean refund(Integer walletDetailId) {
        //1.先获取订单信息
        WalletDetails detail = detailService.getDetailById(walletDetailId);
        Integer payerId = detail.getComeId();
        Integer comeId = detail.getPayerId();
        //2.将支付方和收款方互换，再次进行支付操作
        Boolean pay = pay(payerId, detail.getAmount(), comeId);
        if (pay){
            WalletDetails walletDetails = new WalletDetails();
            walletDetails.setAmount(detail.getAmount());//金额
            walletDetails.setComeId(comeId);//收款方
            walletDetails.setPayerId(payerId);//付款方
            walletDetails.setOneType(1);//操作成功
            walletDetails.setOperationTime(new Date(System.currentTimeMillis()));//时间
            walletDetails.setOperationType(2);//退款
            Integer integer = detailService.insertWalletDetail(walletDetails);
            if (integer == 1){
                pay = true;
            }
        }
        return pay;

    }

    @Override
    public Wallet getBalance(Integer userId) {
        QueryWrapper<Wallet> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Wallet wallet = walletMapper.selectOne(wrapper);
        return wallet;
    }
}
